const { ccclass, property } = cc._decorator;

@ccclass
export default abstract class UIBase extends cc.Component {

    params = null;

    protected onLoad(): void {
        this.node.x = 0;
        this.node.y = 0;
        this.node.width = cc.winSize.width;
        this.node.height = cc.winSize.height;

        let widget = this.node.getComponent(cc.Widget);
        if(!widget){
            widget = this.node.addComponent(cc.Widget);
            widget.alignMode = cc.Widget.AlignMode.ALWAYS;
            widget.bottom = widget.top = widget.left = widget.right = 0;
        }
        let blockInput = this.node.getComponent(cc.BlockInputEvents);
        if(!blockInput) this.node.addComponent(cc.BlockInputEvents);
    }

    /**
     * 初始化ui
     * @param args 
     */
    abstract init(...args);

    /**
     * 展示ui
     * @param options 
     */
    public show(options?: any) {
        this.init(options);
    }

    /**
     * 隐藏ui
     */
    public hide() {
        this.finishCallback && this.finishCallback(false);
    }


    protected onDestroy(): void {
        //回收
        this.finishCallback && this.finishCallback(false);
    }

    /**
     * UI流程结束回调（注意：该回调为 UIManager 专用，重写 hide 函数时记得调用该回调）
     */
    protected finishCallback: (suspended: boolean) => void = null;

    /**
     * 设置UI完成回调（该回调为 UIManager 专用）
     * @param callback 回调
     */
    public setFinishCallback(callback: (suspended: boolean) => void) {
        this.finishCallback = callback;
    }

}